import lombok.SneakyThrows;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author 乐心湖
 * @version 1.0
 * @date 2021/9/11 0:38
 */

public class GUI {

    public static ObjectOutputStream oos;
    private static boolean flag = true;

    public GUI() throws IOException {
        oos = new ObjectOutputStream(MyClientSocket.getSocket().getOutputStream());
    }

    public static void main(String[] args) throws InterruptedException, IOException {
        Socket client = ConnectedUtils.getConnection();
        MyClientSocket.setSocket(client);

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您的昵称：");
        String name = scanner.nextLine();

        JFrame frame = new JFrame("昵称：" + name);
        frame.setSize(500, 500);
        frame.setLocation(500, 500);
        frame.setContentPane(new JPanel());
        frame.getContentPane().setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextArea board = new JTextArea("");
        JScrollPane js = new JScrollPane(board);
        js.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        js.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        js.setBounds(50, 50, 400, 300);
        frame.getContentPane().add(js);

        JTextArea pf = new JTextArea("");
        JScrollPane js2 = new JScrollPane(pf);
        js2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        js2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        js2.setBounds(50, 350, 250, 100);
        frame.getContentPane().add(js2);

        JButton b = new JButton("发送");
        b.setBounds(300, 375, 100, 50);
        b.addActionListener(new ActionListener() {
            @SneakyThrows
            @Override
            public void actionPerformed(ActionEvent e) {

                oos.writeObject(pf.getText());
                //board.append("你说：" + pf.getText() + "\r\n");
                board.setCaretPosition(board.getText().length());
                pf.setText("");

            }
        });
        frame.getContentPane().add(b);
        frame.setVisible(true);
        new Thread(new ClientRecv2(board, name)).start();
        while (true) {
            if (flag || MyClientSocket.isOosUpdated()) {
                oos = new ObjectOutputStream(MyClientSocket.getSocket().getOutputStream());
                oos.writeObject(name);
                flag = false;
                MyClientSocket.setOosUpdated(false);
            }
        }
    }
}

class ClientRecv2 implements Runnable {
    private ObjectInputStream ois;
    private boolean flag = true;
    private boolean isConnected = true;
    private JTextArea board;
    private String name;

    public ClientRecv2(JTextArea board, String name) {
        this.board = board;
        this.name = name;
    }

    @SneakyThrows
    @Override
    public void run() {
        System.out.println("欢迎来到聊天室");
        while (true) {
            if (flag) {
                ois = new ObjectInputStream(MyClientSocket.getSocket().getInputStream());
                flag = false;
                this.isConnected = true;
            }
            if (isConnected) {
                try {
                    board.append(ois.readObject() + "\r\n");
                } catch (IOException | ClassNotFoundException e) {
                    this.isConnected = false;
                }
            } else {
                board.append("服务器掉线了，正在重连中\r\n");
                MyClientSocket.setSocket(ConnectedUtils.getConnection());
                //board.append("重新连接上啦，点一下发送噢\r\n");
                GUI.oos = new ObjectOutputStream(MyClientSocket.getSocket().getOutputStream());
                GUI.oos.writeObject(name);
                this.flag = true;
            }
        }
    }
}
